<?php
/**
 * Created by PhpStorm.
 * User: Celebration-Grey
 * Date: 2017/5/9
 * Time: 14:40
 */

namespace App\Http\Classes;


abstract class Original
{
    private $src;
    private $name;
    private $host;
    private $url;
    private $type;
    private $extension;
    private $msg;
    private $code;
    public $original;

    public function __construct($file)
    {
        $this -> setName();
        $this -> setHost();
        $this -> setFile($file);
    }

    public abstract function setOriginal();

    private function setName(){
        $this -> name = hash('md5',date('YmdHis').$_COOKIE['user_id']);
    }

    private function setHost(){
        $this -> host = 'http://'.$_SERVER['HTTP_HOST'].'/';
    }

    private function setFile($file){
        $errorCode = $file['error'];

        if ($errorCode === UPLOAD_ERR_OK) {
            $type = $file['type'];

            if($type == 'image/jpeg'){
                $type = IMAGETYPE_JPEG;
            }
            else if ($type == 'image/png'){
                $type = IMAGETYPE_PNG;
            }
            else if ($type == 'image/gif'){
                $type = IMAGETYPE_GIF;
            }
            else{
                $type = false;
            }

            if ($type){
                $extension = image_type_to_extension($type);
                $this->setOriginal();
                $src = $this->original.$this->name.$extension;

                if ($type == IMAGETYPE_GIF || $type == IMAGETYPE_JPEG || $type == IMAGETYPE_PNG){

                    if (file_exists($src)){
                        unlink($src);
                    }

                    $result = move_uploaded_file($file['tmp_name'],$src);

                    if ($result){
                        $this -> src = $src;
                        $this -> url = $this -> host . $this -> src;
                        $this -> type = $type;
                        $this -> extension = $extension;
                        $this -> code = 200;
                        $this -> msg = 'Upload and save file success';
                    } else{
                        $this -> code = 304;
                        $this -> msg = 'Failed to save file';
                    }
                } else{
                    $this -> code = 304;
                    $this -> msg = 'Please upload image with the following types: JPG, PNG, GIF';
                }
            } else{
                $this -> code = 304;
                $this -> msg = 'Please upload image file';
            }
        } else{
            $this -> code = 500;
            $this -> msg = $this -> codeToMessage($errorCode);
        }
    }

    private function codeToMessage($code) {
        $errors = array(
            UPLOAD_ERR_INI_SIZE =>'The uploaded file exceeds the upload_max_filesize directive in php.ini',
            UPLOAD_ERR_FORM_SIZE =>'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
            UPLOAD_ERR_PARTIAL =>'The uploaded file was only partially uploaded',
            UPLOAD_ERR_NO_FILE =>'No file was uploaded',
            UPLOAD_ERR_NO_TMP_DIR =>'Missing a temporary folder',
            UPLOAD_ERR_CANT_WRITE =>'Failed to write file to disk',
            UPLOAD_ERR_EXTENSION =>'File upload stopped by extension',
        );

        if (array_key_exists($code, $errors)) {
            return $errors[$code];
        }

        return 'Unknown upload error';
    }

    public function getResult(){
        $data = [];
        $data['src'] = $this -> src;
        $data['url'] = $this -> url;
        $data['name'] = $this -> name;
        $data['type'] = $this -> type;
        $data['extension'] = $this -> extension;
        return $data;
    }

    public function getMsg(){
        return $this -> msg;
    }

    public function getCode(){
        return $this -> code;
    }
}